/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess
 * @author:LiBo/Alex
 * @create-date:2020-05-19 9:56
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class UserRestApplication extends SpringBootServletInitializer {

    /**
     * main方法执行操作处理功能
     * @param args
     */
    public static void main(String[] args){
        SpringApplication run = new SpringApplicationBuilder().application();
        run.setAllowBeanDefinitionOverriding(true);
        run.setSources(Sets.newHashSet(UserRestApplication.class.getName()));
        ConfigurableApplicationContext annotationConfigWebApplicationContext = (ConfigurableApplicationContext) run.run(args);
//        run.setAllowBeanDefinitionOverriding(true);
//        run.setAllowCircularReferences(true);
    }

    /**
     * 启动服务处理操作
     * @param servletContext
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext rootApplicationContext = createRootApplicationContext(servletContext);
        if (null != rootApplicationContext) {
            /**
             * @see WebUtil#getRequest()
             * @see WebUtil#getResponse()
             */
            servletContext.addListener(new RequestContextListener());
            log.info("init org.springframework.web.context.request.RequestContextListener...");
        }
    }
}
